/*
 * Copyright (C) 2013 Daniel Wasilew <daniel.wasilew@codeminer.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.codeminer.parts.update;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author Daniel Wasilew <daniel.wasilew@codeminer.org>
 */
public final class Version implements Comparable<Version> {

    private static final Logger log = LoggerFactory.getLogger(Version.class);
    public static final Version NO_UPDATE = new Version(-1, "NO_UPDATE", "never");

    public static Version current() {
        return new Version();
    }

    public static Version custom(String version, long buildId, String buildTime) {
        return new Version(buildId, version, buildTime);
    }

    private long buildId;
    private String version;
    private String buildTime;

    private Version() {
        initValuesFromResource();
    }

    private Version(long buildId, String version, String buildTime) {
        this.buildId = buildId;
        this.version = version;
        this.buildTime = buildTime;
    }

    @Override
    public int compareTo(Version o) {
        return (int) (this.buildId - o.buildId);
    }

    public long getBuildId() {
        return buildId;
    }

    public String getBuildTime() {
        return buildTime;
    }

    public String getVersion() {
        return version;
    }

    private void initValuesFromResource() {
        InputStream resourceAsStream = Version.class.getResourceAsStream("/version.properties");
        Properties properties = new Properties();
        try {
            properties.load(resourceAsStream);
            version = properties.getProperty("version");
            String buildId = properties.getProperty("buildId", "0");
            try {
                this.buildId = Long.parseLong(buildId);
            } catch (NumberFormatException ex) {
                log.info("could not parse build id [{}]", buildId);
                this.buildId = 0;
            }
            buildTime = properties.getProperty("buildTime");
        } catch (IOException ex) {
            log.error("could not read version informations", ex);
        }

    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Version other = (Version) obj;
        if (this.buildId != other.buildId) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 83 * hash + (int) (this.buildId ^ (this.buildId >>> 32));
        return hash;
    }

    @Override
    public String toString() {
        return "Version{version=" + version + ", buildId=" + buildId + ", buildTime=" + buildTime + '}';
    }
}
